<?php

namespace app\admin\controller;

use app\admin\library\DbLink;
use app\common\controller\Backend;
use app\admin\library\DbMap;
use app\admin\model\MwColumn as columnM;
use app\admin\model\MwModule as moduleM;
use app\admin\model\MwConnection as connectionM;
use app\admin\model\MwDb as dbM;

/**
 * 字段
 *
 * @icon fa fa-circle-o
 */
class MwColumn extends Backend
{

    /**
     * MwColumn模型对象
     * @var \app\admin\model\MwColumn
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\MwColumn;
        $this->view->assign("isQuickSearchList", $this->model->getIsQuickSearchList());
        $this->view->assign("isMultipleList", $this->model->getIsMultipleList());
        $this->view->assign("searchRuleList", $this->model->getSearchRuleList());
        $this->view->assign("searchList", $this->model->getSearchList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     * @return string|\think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $moduleId = $this->request->get('module_id');
            $where1['module_id']=$moduleId;

            $list = $this->model
                ->where($where)
                ->where($where1)
//                ->order($sort, $order)
                ->order($sort)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加字段到数据库
     * @param $connect
     * @param $module_id
     * @param $table_name
     * @return array|bool|false
     * @throws \Exception
     */
    public function insertData($connect,$module_id,$table_name,$database){
        //数据表schema信息
        $tableInfo = DbLink::getStructure($connect,$table_name,$database);
        $data = [];
//        var_dump($tableInfo);
        foreach($tableInfo as $k=>$v){
            $data[$k]['module_id'] = $module_id;
            $data[$k]['name'] = $v['COLUMN_NAME']; //字段
            $data[$k]['description'] = $v['COLUMN_COMMENT'];//字段注释
//            $data[$k]['input_type']=substr($v['Type'],0,strpos($v['Type'],'('));//类型
            $data[$k]['input_type'] = $v['DATA_TYPE'];//类型
            $data[$k]['output_type'] = DbMap::parseType($data[$k]['name'], $data[$k]['input_type']);
            $data[$k]['length'] = $v['CHARACTER_MAXIMUM_LENGTH'];//字段长度
            if (!$data[$k]['length']){
                $data[$k]['length'] = strstr(substr($v['COLUMN_TYPE'],strripos($v['COLUMN_TYPE'],"(")+1), ')', true);//字段长度，
                if ($data[$k]['input_type'] == 'float'){
                    //字段长度，包含小数点时为[10,5]
                    $data[$k]['length'] = explode(',', $data[$k]['length']);
                }
            }
            $data[$k]['default']= $v['COLUMN_DEFAULT'] ? $v['COLUMN_DEFAULT'] : '';//默认值，默认为空
            $data[$k]['is_null']= $v['IS_NULLABLE'] == 'NO' ? 'false' : 'true';//是否允许为空，默认为true
            $data[$k]['auto_increment'] = $v['EXTRA'] ? 'true' : 'false';
            $data[$k]['is_primary'] = $v['COLUMN_KEY'] ? 'true' : 'false';
        }

//        var_dump($data);
        $column = new columnM();
        return $column->saveAll($data);

    }

}
